package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.BaseFileTable;
import com.ruoyi.system.service.IBaseFileTableService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * baseFileController
 * 
 * @author wcc
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/baseFile")
public class BaseFileTableController extends BaseController
{
    @Autowired
    private IBaseFileTableService baseFileTableService;
    @Autowired
    private RemoteFileService remoteFileService;
    /**
     * 查询baseFile列表
     */
    //@RequiresPermissions("system:baseFile:list")
    @GetMapping("/list")
    public TableDataInfo list(BaseFileTable baseFileTable)
    {
        startPage();
        List<BaseFileTable> list = baseFileTableService.selectBaseFileTableList(baseFileTable);
        return getDataTable(list);
    }

    /**
     * 导出baseFile列表
     */
    @RequiresPermissions("system:baseFile:export")
    @Log(title = "baseFile", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseFileTable baseFileTable)
    {
        List<BaseFileTable> list = baseFileTableService.selectBaseFileTableList(baseFileTable);
        ExcelUtil<BaseFileTable> util = new ExcelUtil<BaseFileTable>(BaseFileTable.class);
        util.exportExcel(response, list, "baseFile数据");
    }

    /**
     * 获取baseFile详细信息
     */
    @RequiresPermissions("system:baseFile:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseFileTableService.selectBaseFileTableById(id));
    }

    /**
     * 新增baseFile
     */
    @RequiresPermissions("system:baseFile:add")
    @Log(title = "baseFile", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseFileTable baseFileTable)
    {
        return toAjax(baseFileTableService.insertBaseFileTable(baseFileTable));
    }

    /**
     * 修改baseFile
     */
    @RequiresPermissions("system:baseFile:edit")
    @Log(title = "baseFile", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseFileTable baseFileTable)
    {
        return toAjax(baseFileTableService.updateBaseFileTable(baseFileTable));
    }

    /**
     * 删除baseFile
     */
    @RequiresPermissions("system:baseFile:remove")
    @Log(title = "baseFile", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseFileTableService.deleteBaseFileTableByIds(ids));
    }

    @Log(title = "上传文件", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file)
    {
        if (!file.isEmpty())
        {
            R<SysFile> fileResult = remoteFileService.upload(file,"ruirui");
            String url = fileResult.getData().getUrl();
            return AjaxResult.success(url);
        }
        return error("上传异常，请联系管理员");
    }
}
